﻿using MySql.Data.MySqlClient;
using Service.common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Newtonsoft.Json.Linq;
using Service.model;

namespace Service.utils
{
    public class ResultUtil
    {
        #region 查询返回DataTable
        /// <summary>
        /// 查询返回DataTable
        /// </summary>
        /// <typeparam name="T">泛型类</typeparam>
        /// <param name="sqlStr">sql语句</param>
        /// <returns>DataTable</returns>
        public static DataTable getDataTable(string sqlStr)
        {
            //创建数据库连接
            MySqlConnection conn = new MySqlConnect().getConnection(new MySqlConnect().connection());
            //执行数据库查询操作
            DataTable dt = MySqlHelper.ExecuteDataset(conn, sqlStr).Tables[0];
            //关闭释放连接资源
            conn.Close();
            //返回DataTable
            return dt;
        }
        #endregion

        #region 返回规范的结果集
        /// <summary>
        /// 返回规范的结果集
        /// </summary>
        /// <param name="status"></param>
        /// <param name="message"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getStandardResult(int status, string message, object obj)
        {
            Result result = new Result();
            result.status = status.ToString();
            result.message = message;
            result.result = obj;
            return JsonConvert.SerializeObject(result);
        }
        #endregion

        #region 新增、修改、删除操作
        /// <summary>
        /// 新增、修改、删除操作
        /// </summary>
        /// <param name="sqlStr">sql字符</param>
        /// <returns>无</returns>
        public static int insOrUpdOrDel(string sqlStr)
        {
            //定义影响条数
            int numb = 0;
            //创建数据库连接
            MySqlConnection conn = new MySqlConnect().getConnection(new MySqlConnect().connection());
            //启动一个事务
            MySqlTransaction myTrans = conn.BeginTransaction();
            //事务命令
            MySqlCommand cmd = conn.CreateCommand();
            //事务命令
            cmd.Transaction = myTrans;
            try
            {
                //执行数据库新增、修改、删除操作
                numb = MySqlHelper.ExecuteNonQuery(conn, sqlStr);
                //事务提交
                myTrans.Commit();
            }
            catch (Exception)
            {
                //异常回滚
                myTrans.Rollback();
                throw;
            }
            //关闭释放连接资源
            conn.Close();
            return numb;
        }
        #endregion

        #region 数据库参数式新增、修改、删除操作
        /// <summary>
        /// 数据库参数式新增、修改、删除操作
        /// </summary>
        /// <param name="sqlStr">sql字符</param>
        /// <returns>无</returns>
        public static void insOrUpdOrDel(string sqlStr, MySqlParameter[] commandParameters)
        {
            //创建数据库连接
            MySqlConnection conn = new MySqlConnect().getConnection(new MySqlConnect().connection());
            //启动一个事务
            MySqlTransaction myTrans = conn.BeginTransaction();
            //事务命令
            MySqlCommand cmd = conn.CreateCommand();
            //事务命令
            cmd.Transaction = myTrans;
            try
            {
                //执行数据库新增、修改、删除操作
                MySqlHelper.ExecuteNonQuery(conn, sqlStr, commandParameters);
                //事务提交
                myTrans.Commit();
            }
            catch (Exception)
            {
                //异常回滚
                myTrans.Rollback();
            }
            //关闭释放连接资源
            conn.Close();
        }
        #endregion
    }
}